﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    abstract class Shape
    {
        abstract public double Area();//抽象方法，计算面积
        abstract public bool IsLegal();//抽象方法，判断形状是否合法
    }

    //长方形类，继承Shape类
    class Rectangle : Shape
    {
        private double length;//长方形的长
        private double width;//宽

        public Rectangle(double length, double width)
        {
            if (length == 0 || width == 0)
            {
                Console.WriteLine("数据非法");
                return;
            }
            this.length = length;
            this.width = width;
        }

        public double Length
        {
            get { return length; }
            set { length = value; }
        }

        public double Width
        {
            get { return width; }
            set { width = value; }
        }

        public override double Area()
        {
            return length * width;
        }

        public override bool IsLegal()
        {
            if (length == 0 || width == 0)//长或宽为0是不合法
                return false;
            return true;
        }
    }

    //正方形类，继承Shape类
    class Square : Shape
    {
        private double a;

        public double A
        {
            get { return a; }
            set { a = value; }
        }

        public Square(double a)
        {
            if (a == 0)
            {
                Console.WriteLine("数据非法");
                return;
            }
            this.a = a;
        }
        public override double Area()
        {
            return a * a;
        }

        public override bool IsLegal()
        {
            return !(a == 0);
        }
    }

    //三角形类，继承Shape类
    class Triangle : Shape
    {
        private double x;//三角形的三边
        private double y;
        private double z;

        public double X
        {
            get { return x; }
            set { x = value; }
        }
        public double Y
        {
            get { return y; }
            set { y = value; }
        }
        public double Z
        {
            get { return z; }
            set { z = value; }
        }
        public Triangle(double x, double y, double z)
        {
            if (x + y > z && x + z > y && y + z > x)
            {
                this.x = x;
                this.y = y;
                this.z = z;
            }
            else
            {
                Console.WriteLine("数据非法");
            }
        }
        public override double Area()
        {
            double C = (x + y + z) / 2.0;
            return Math.Sqrt(C * (C - x) * (C - y) * (C - z));//海伦公式求三角形的面积 
        }

        public override bool IsLegal()
        {
            if (x + y > z && x + z > y && y + z > x)
                return true;
            return false;
        }
    }

    //工厂类
    class ShapeFactory
    {
        
        public Shape GetShape(int i, double x, double y, double z)
        {
            if (i == 0)
                return new Rectangle(x, y);
            else if (i == 1)
                return new Square(x);
            else if (i == 2)
                return new Triangle(x, y, z);
            return null;
        }

    }
    internal class Program
    {
        static void Main(string[] args)
        {
            ShapeFactory factory = new ShapeFactory();//工厂对象
            Random rand = new Random(); //随机数对象，用来产生随机数
            Shape[] shapes=new Shape[10];
            double sum = 0d;
            for(int i = 0; i < 10; i++)//产生10个随机形状的对象，边的长度为0到10的随机数
            {
                shapes[i]= factory.GetShape(rand.Next(3), 10d * rand.NextDouble(), 10d * rand.NextDouble(), 10d * rand.NextDouble());
                Console.WriteLine("该图形的面积为"+shapes[i].Area());
                sum += shapes[i].Area();
            }
            Console.WriteLine("这十个图形面积之和为" + sum);
        }
    }
}
